#include<iostream>
#include<vector>
using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left),right(right) {}   
};


TreeNode* _buildTree(vector<int>& preorder, vector<int>& inorder, int& prei,
    int inbegin, int inend) {
    if (inbegin > inend)
        return nullptr;

    TreeNode* root = new TreeNode(preorder[prei]);

    int rooti = inbegin;
    while (rooti <= inend) {
        if (preorder[prei] == inorder[rooti])
            break;
        else
            rooti++;
    }
    prei++;

    root->left = _buildTree(preorder, inorder, prei, inbegin, rooti - 1);
    root->right = _buildTree(preorder, inorder, prei, rooti + 1, inend);

    return root;
}

TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
    int i = 0;
    return _buildTree(preorder, inorder, i, 0, inorder.size() - 1);
}

int main()
{
    vector<int> preorder = { 3, 9, 20, 15, 7}, inorder = { 9, 3, 15, 20, 7};
    buildTree(preorder, inorder);
    return 0;
}